package com.slkj.shilixiaoyuanapp.ui.parent.tool;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.LazyFragment;
import com.slkj.shilixiaoyuanapp.entity.MainToolEntity;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.activities.ActivitysActivity;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.bodyartspecialty.BodyartspecialtyActivity;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.exercise.ExerciseActivity;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.game.GameActivity;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.homework.HomeWorkActivity;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.leave.LeaveActivity;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.phase.PhaseActivity;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.reading.ReadingActivity;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.readingaloud.ReadAloudActivity;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.story.StoryActivity;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.unittest.UnitTestActivity;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.association.AssociationActivity;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.schoolmuseum.SchoolMuseumActivity;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindArray;
import butterknife.BindView;

/**
 * Created  on 2018-07-15
 *
 * @author 何磊
 * @describe 家长首页工具fragment
 */
public class ParentToolFragment extends LazyFragment {
    @BindView(R.id.recycer)
    RecyclerView recycerView;
    @BindArray(R.array.parent_main_tool_level1)
    String[] oneTools;
    int[] oneImgs = {R.drawable.ballet, R.drawable.tennis, R.drawable.read, R.drawable.balloons, R.drawable.vacation, R.drawable.tape, R.drawable.test,R.drawable.stairs,R.drawable.milkbottle,R.drawable.castle,R.drawable.moon};
    Class[] oneToolsActivitys = {BodyartspecialtyActivity.class, ExerciseActivity.class, ReadingActivity.class, ActivitysActivity.class, LeaveActivity.class, ReadAloudActivity.class, UnitTestActivity.class,PhaseActivity.class,HomeWorkActivity.class,GameActivity.class,StoryActivity.class};
    @BindArray(R.array.parent_main_tool_level2)
    String[] twoTools;
    int[] twoImgs = {R.drawable.tool_bicycle, R.drawable.tool_museum};
    Class[] twoToolsActivitys = {AssociationActivity.class, SchoolMuseumActivity.class};
    private static final int SPAN_COUNT = 4;
    BaseMultiItemQuickAdapter adapter;
    List<MainToolEntity> toolEntityList = new ArrayList<>();

    @Override
    protected int getResId() {
        return R.layout.fragment_tools;
    }

    @Override
    protected void onRealLoaded() {
        initToolsData();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), SPAN_COUNT);
        adapter = new ToolAdapter(toolEntityList);
        recycerView.setAdapter(adapter);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //footview
                if (position >= toolEntityList.size()) {
                    return SPAN_COUNT;
                }
                return toolEntityList.get(position).getItemType() == MainToolEntity.TYPE_HEAD ? SPAN_COUNT : 1;
            }
        });
        recycerView.setLayoutManager(gridLayoutManager);
        View view = getLayoutInflater().inflate(R.layout.footer_view, (ViewGroup) recycerView.getParent(), false);
        //adapter.addFooterView(view);
    }

    /**
     * 将工具分组
     */
    private void initToolsData() {
        for (int i = 0; i < oneTools.length; i++) {
            toolEntityList.add(new MainToolEntity(1001+i,false, oneTools[i], oneToolsActivitys[i], oneImgs[i]));
        }
        toolEntityList.add(new MainToolEntity(true));
        for (int i = 0; i < twoTools.length; i++) {
            if (i == 0){
                toolEntityList.add(new MainToolEntity(1000, false, twoTools[i], twoToolsActivitys[i], twoImgs[i]));

            }else {
                toolEntityList.add(new MainToolEntity(1008 + i, false, twoTools[i], twoToolsActivitys[i], twoImgs[i]));
            }
        }
        //toolEntityList.add(new MainToolEntity(true));
    }

    public class ToolAdapter extends BaseMultiItemQuickAdapter<MainToolEntity, BaseViewHolder> {
        public ToolAdapter(List<MainToolEntity> data) {
            super(data);
            addItemType(MainToolEntity.TYPE_ITEN, R.layout.item_main_tool);
            addItemType(MainToolEntity.TYPE_HEAD, R.layout.item_main_tool_head);
        }
        @Override
        protected void convert(BaseViewHolder helper, MainToolEntity item) {
            switch (helper.getItemViewType()) {
                case MainToolEntity.TYPE_HEAD:
//                    helper.setText(R.id.tv_name, item.getName());
                    break;
                case MainToolEntity.TYPE_ITEN:
                    helper.setText(R.id.tv_name, item.getName());
                    helper.setImageResource(R.id.img, item.getRes());
                    helper.itemView.setOnClickListener(v -> {
                        startActivity(item.getActivityClass());
                    });
                    break;
            }
        }
    }
}
